<?php

declare(strict_types=1);

namespace App\AdminApi\Modular\Data\Service;

use App\Common\Modular\Data\Model\BannerModel;
use App\Common\Modular\Data\Serve\BannerServe;
use Zms\Exception\AppException;
use Core\Package\ElementPlus\FormCreate\Form;
use Core\Package\ElementPlus\FormCreate\FormItem;

/**
 * 轮播服务层
 * Class BannerService
 */
class BannerService extends BannerServe
{
    /**
     * 创建轮播-表单
     * @return Form
     */
    public function createdBannerForm(): Form
    {
        $rule[] = FormItem::input('title', '轮播标题')->maxlength(50)->placeholder('输入轮播标题');
        $rule[] = FormItem::Select('position', '显示位置')->options($this->getClassify())->required('请选择显示位置');
        $rule[] = FormItem::input('url', '跳转链接')->maxlength(128)->placeholder('点击轮播跳转地址');
        $rule[] = FormItem::imageInput('image', '轮播图')->required('请上传轮播图');
        $rule[] = FormItem::Color('color', '图片主色')->hint('设置可以配合前端实现更好的页面效果');
        $rule[] = FormItem::DataTime('start', '开始时间')->hint('不设置则是立即开始')->col(12);
        $rule[] = FormItem::DataTime('end', '结束时间')->hint('不设置则一直有效')->col(12);
        $rule[] = FormItem::number('sort', '轮播排序')->placeholder('越大越靠前')->style(['width' => '100%'])->col(12);
        $rule[] = FormItem::Switchs('status', '显示状态', '1')->col(12);
        return Form::create('添加轮播')->setRules($rule)->setOptionFormInline(true);
    }


    /**
     * 创建轮播-表单
     * @param string $id
     * @return Form
     * @throws AppException
     */
    public function updatedBannerForm(string $id = ''): Form
    {
        /**
         * @var BannerModel $banner
         */
        $banner = container(BannerService::class)->search(['id' => $id])->first();
        if ($banner && $id) {
            $rule[] = FormItem::Hidden('id', $id);
            $rule[] = FormItem::input('title', '轮播标题', $banner->title)->maxlength(50)->placeholder('输入轮播标题');
            $rule[] = FormItem::Select('position', '显示位置', $banner->position)->options($this->getClassify())->required('请选择显示位置');
            $rule[] = FormItem::input('url', '跳转链接', $banner->url)->maxlength(128)->placeholder('点击轮播跳转地址');
            $rule[] = FormItem::imageInput('image', '轮播图', $banner->image)->required('请上传轮播图');
            $rule[] = FormItem::Color('color', '图片主色',$banner->color)->hint('设置可以配合前端实现更好的页面效果');
            $rule[] = FormItem::DataTime('start', '开始时间', $banner->start ? date("Y-m-d H:i:s", $banner->start) : '')->hint('不设置则是立即开始')->col(12);
            $rule[] = FormItem::DataTime('end', '结束时间', $banner->end ? date("Y-m-d H:i:s", $banner->end) : '')->hint('不设置则一直有效')->col(12);
            $rule[] = FormItem::number('sort', '轮播排序', $banner->sort)->placeholder('越大越靠前')->style(['width' => '100%'])->col(12);
            $rule[] = FormItem::Switchs('status', '显示状态', (string)$banner->status)->col(12);
            return Form::create('编辑轮播')->setRules($rule)->setOptionFormInline(true);
        }
        throw new AppException('轮播已删除');
    }
}